package org.intellij.plugins.junit4;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.project.Project;


/**
 * Runs a task that does something to modify a class file / editor. Task is a runnable,
 * is executed by a CommandProcessor inside a writeAction.
 * <p/>
 * Tasks that write to an editor seem to need to be executed in a
 * CommandProcesor.executeCommand, which itself is executed in an
 * Application.runWriteAction().
 * <p/>
 * Does the jerb of wrapping up in the double-runner stupidity.
 */
public class WriteExecutor implements Runnable
{
    public static void run(Project project, String description, Runnable task)
    {
        new WriteExecutor(project, description, task).run();
    }

    private final Project project;
    private final Runnable task;
    private final String description;

    public WriteExecutor(Project project, String description, Runnable task)
    {
        this.project = project;
        this.task = task;
        this.description = description;
    }

    public void run()
    {
        ApplicationManager.getApplication().runWriteAction(
                new Runnable()
                {
                    public void run()
                    {
                        CommandProcessor cp = CommandProcessor.getInstance();
                        cp.executeCommand(project, task, description, "group id?");

                    }
                }
        );

    }
}
